//
//  HYFootRefreshView.m
//  HYRefresh
//
//  Created by HYMac on 16/3/15.
//  Copyright © 2016年 HYMac. All rights reserved.
//

#import "HYFootRefreshView.h"

@interface HYFootRefreshView()
@property(nonatomic, weak)UIView *contentV;
@property(nonatomic, weak)UIImageView *arrowView;
@property(nonatomic, weak)UILabel *stateLabel;
@end
@implementation HYFootRefreshView
-(void)setState:(refreshState)state
{
    _state=state;
    if (state==refreshStateNoneNormal)
    {
        [self.contentV removeFromSuperview];
    }
    else if (state==refreshStateLowNormal)
    {
        //self.stateLabel.text=refreshStateLowNormalText;
        self.stateLabel.text=refreshStateLowNormalText;
        
        self.arrowView.image=[UIImage imageNamed:@"arrow@2x"];
        self.arrowView.transform=CGAffineTransformIdentity;
    }
    else if (state==refreshStateHeightNormal)
    {
        
        //  self.stateLabel.text=refreshStateHeightNormalText;
        self.stateLabel.text=refreshStateHeightNormalText;
        self.arrowView.image=[UIImage imageNamed:@"arrow@2x"];
        self.arrowView.transform=CGAffineTransformMakeScale(1, -1);
    }
    else if (state==refreshStatePulling)
    {
        
    }
    else
    {
        self.stateLabel.text=refreshStateRefreshingText;
        _scrollView.contentInset=UIEdgeInsetsMake(50, 0, 0, 0);
        UIActivityIndicatorView *activity=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activity.frame=self.arrowView.frame;
        //   activity.backgroundColor=[UIColor blueColor];
        //activity.color=[UIColor redColor];
        //    self.stateLabel.text=refreshStateRefreshingText;
        [self.arrowView removeFromSuperview];
        
        [self.contentV addSubview:activity];
        [activity startAnimating];
        [self.target performSelector:self.action withObject:nil];
    }

}
-(UIView *)contentV
{
    if (_contentV==nil)
    {
        UIView *contentV=[[UIView alloc]initWithFrame:CGRectMake(0, -50, IOS_screen.width, 50)];
        
        [_scrollView addSubview:contentV];
        _contentV=contentV;
    }
    
    return _contentV;
}
-(UIImageView *)arrowView
{
    if (_arrowView==nil)
    {
        CGFloat imageW=20;
        CGFloat imageH=40;
        UIImageView *imageV=[[UIImageView alloc]initWithFrame:CGRectMake(IOS_screen.width*0.35, self.contentV.frame.size.height*0.1, imageW, imageH)];
        
        _arrowView=imageV;
        //  imageV.backgroundColor=[UIColor blackColor];
        [self.contentV addSubview:imageV];
    }
    
    return _arrowView;
}
-(UILabel *)stateLabel
{
    if (_stateLabel==nil)
    {
        CGFloat labelW=self.contentV.frame.size.width*0.4;
        CGFloat labelH=self.contentV.frame.size.height*0.8;
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(IOS_screen.width*0.45, self.contentV.frame.size.height*0.1, labelW, labelH)];
       // label.backgroundColor=[UIColor yellowColor];
       // label.backgroundColor=[UIColor blackColor];
        [self.contentV addSubview:label];
        _stateLabel=label;
    }
    return _stateLabel;
}
//

-(void)setScrollView:(UIScrollView *)scrollView
{
    _scrollView=scrollView;
    [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if (!(self.state==refreshStateRefreshing))
    {
    
    if ((_scrollView.contentOffset.y<0)&&(_scrollView.contentOffset.y>-50))
    {
        self.state=refreshStateLowNormal;
        self.arrowView.image=[UIImage imageNamed:@"arrow@2x"];
        self.arrowView.transform=CGAffineTransformIdentity;
    }
    else if (_scrollView.contentOffset.y<-50)
    {
        if (_scrollView.decelerating)
        {
            self.state=refreshStateRefreshing;
            return;
        }
        self.state=refreshStateHeightNormal;
        self.arrowView.transform=CGAffineTransformMakeRotation(M_PI);
    }
    }
}
-(void)dealloc
{
    [_scrollView removeObserver:self forKeyPath:@"contentOffset"];
}
@end
